package com.basin.auth.openapi;

import cn.dev33.satoken.annotation.SaCheckHttpBasic;
import com.basin.api.vo.UriAuthConfigVO;
import com.basin.auth.core.component.AuthServerManager;
import com.basin.common.core.pojo.BasinNode;
import com.basin.common.core.response.Resp;
import com.basin.common.satoken.annotation.IgnoreGlobalAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 * @date: 2025/3/24 20:51
 * @description:
 */
@RestController
@Slf4j
@RequestMapping("/open-api")
@RequiredArgsConstructor
@IgnoreGlobalAuth
@SaCheckHttpBasic(account = "basin_auth:123456")
@Tag(name = "鉴权服务OpenApi")
public class AuthOpenApi {
    private final AuthServerManager authServerManager;

    @GetMapping("/get-perms-by-login-id")
    @Operation(summary = "通过登录ID获取权限集合")
    @Parameter(name = "loginId", required = true, description = "用户ID")
    public Resp<List<String>> getPermsByLoginId(@RequestParam(name = "loginId") Object loginId) {
        return Resp.success(authServerManager.getPermsByLoginId(loginId));
    }

    @GetMapping("/get-roles-by-login-id")
    @Operation(summary = "通过登录ID获取角色集合")
    @Parameter(name = "loginId", required = true, description = "用户ID")
    public Resp<List<String>> getRolesByLoginId(@RequestParam(name = "loginId") Object loginId) {
        return Resp.success(authServerManager.getRolesByLoginId(loginId));
    }

    @GetMapping("/get-white-userIds")
    @Operation(summary = "获取白名单用户ID集合")
    public Resp<List<String>> getWhiteUserIds() {
        return Resp.success(authServerManager.getWhiteUserIds());
    }

    @GetMapping("/get-white-ips")
    @Operation(summary = "获取白名单IP集合")
    public Resp<List<String>> getWhiteIps() {
        return Resp.success(authServerManager.getWhiteIps());
    }

    @GetMapping("/get-white-uris")
    @Operation(summary = "获取白名单请求路径URI集合")
    public Resp<List<String>> getWhiteUris() {
        return Resp.success(authServerManager.getWhiteUris());
    }

    @GetMapping("/get-uri-auth-config")
    @Operation(summary = "获取uri的认证配置")
    public Resp<UriAuthConfigVO> getUriAuthConfig(@RequestParam("uriName") String uriName) {
        uriName = "/test";
        UriAuthConfigVO uriAuthConfigVO = new UriAuthConfigVO();
        uriAuthConfigVO.setUri(uriName);
        UriAuthConfigVO.AuthCondition first = new UriAuthConfigVO.AuthCondition(
                "OR", List.of("test", "user"), null
        );
//        UriAuthConfigVO.AuthCondition second = new UriAuthConfigVO.AuthCondition(
//                "OR", List.of("admin", "super_admin"), "AND"
//        );
        BasinNode<UriAuthConfigVO.AuthCondition> node1 =
//                new BasinNode<>(first, new BasinNode<UriAuthConfigVO.AuthCondition>(second));
                new BasinNode<>(first, null);
        uriAuthConfigVO.setRoleCondition(node1);
        return Resp.success(uriAuthConfigVO);
    }
}
